//
//  YTAddCarViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/7/7.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTAddCarViewController.h"
#import "YTCarBandTypeModel.h"
#import "YTCarAddViewModel.h"
#import "YTTeamDriverModel.h"
#import "YTPickerView.h"

#import "YTAssociateDriverViewController.h"
#import "YTSeatTypeViewController.h"
#import "YTCarCertificateViewController.h"
#import "YTSelectCarTypeViewController.h"

@interface YTAddCarViewController ()
@property (weak, nonatomic) IBOutlet UIButton *carOwnerSelf;
@property (weak, nonatomic) IBOutlet UIButton *carOwnerOther;
@property (weak, nonatomic) IBOutlet UITextField *otherNameText;
@property (weak, nonatomic) IBOutlet UILabel *carTypeText;
@property (weak, nonatomic) IBOutlet UITextField *carNumText;
@property (weak, nonatomic) IBOutlet UILabel *carSeatsText;
@property (weak, nonatomic) IBOutlet UILabel *carDateText;
@property (weak, nonatomic) IBOutlet UIButton *carSideImage;
@property (weak, nonatomic) IBOutlet UIButton *carBackImage;
@property (weak, nonatomic) IBOutlet UIButton *carInsideImage;
@property (weak, nonatomic) IBOutlet UITextField *driverName;
@property (weak, nonatomic) IBOutlet UIButton *nextStepBtn;
/** viewmodel*/
@property (strong, nonatomic) YTCarAddViewModel *viewModel;
@end

@implementation YTAddCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _viewModel = [[YTCarAddViewModel alloc] init];
    _nextStepBtn.rac_command = _viewModel.nextStepCommand;
    RAC(_viewModel, carNum) = _carNumText.rac_textSignal;
    RAC(_viewModel, carOwnerName) = _otherNameText.rac_textSignal;
    RAC(_viewModel, carDate) = RACObserve(_carDateText, text);
    @weakify(self);
    [_viewModel.nextStepCommand.enabled subscribeNext:^(NSNumber *enable) {
        @strongify(self);
        self.nextStepBtn.backgroundColor = (enable.boolValue ? kMainBlueColor : [UIColor lightGrayColor]);
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BOOL hidden = (indexPath.row == 2 && _carOwnerSelf.selected == YES);
    return hidden ? 0.1 : 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];
    if (indexPath.row == 6) {
        YTPickerView *picker = [YTPickerView pickerWithType:YTPickerTypeMonthDay];
        picker.dimmedMask = YES;
        picker.birthday = YES;
        picker.tips = @"选择车辆出厂日期";
        picker.completion = ^(NSString *selectString) {
            self.carDateText.text = selectString;
        };
        [picker show];
    }
}

#pragma mark - Action

- (IBAction)carImageSelectAction:(UIButton *)sender {
    @weakify(self);
    [self yt_selecteImageCompletion:^(UIImage * _Nullable image) {
        @strongify(self);
        [sender setBackgroundImage:image forState:UIControlStateNormal];
        switch (sender.tag) {
            case 400:
                self.viewModel.image1 = image;
                break;
            case 401:
                self.viewModel.image2 = image;
                break;
            case 402:
                self.viewModel.image3 = image;
                break;
        }
    }];
}

- (IBAction)isYoutSelfAction:(UIButton *)sender {
    sender.selected = YES;
    _carOwnerOther.selected = NO;
    _viewModel.isOwner = YES;
    [self.tableView reloadData];
}

- (IBAction)isOtherAction:(UIButton *)sender {
    sender.selected = YES;
    _carOwnerSelf.selected = NO;
    _viewModel.isOwner = NO;
    [self.tableView reloadData];
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"YTCarTypeSelect"]) {
        YTSelectCarTypeViewController *selecte = segue.destinationViewController;
        selecte.callBack = [RACSubject subject];
        [selecte.callBack subscribeNext:^(RACTuple *tuple) {
            YTCarBandTypeModel *bandModel = tuple.first;
            YTBandVersionModel *versionModel = tuple.second;
            YTBandColorModel *color = tuple.third;
            // 传递各类id,车型,座位规格,颜色
            self.viewModel.carBand = bandModel.band_id;
            self.viewModel.carVersion = versionModel.ver_id;
            self.viewModel.carColor = color.color_id;
            self.carTypeText.text = [NSString stringWithFormat:@"%@ %@ %@", bandModel.name, versionModel.name, color.name];
        }];
    }
    
    if ([segue.identifier isEqualToString:@"YTSelecteSeatType"]) {
        YTSeatTypeViewController *seatType = segue.destinationViewController;
        seatType.callBack = ^(YTBandVersionModel *model) {
            self.carSeatsText.text = model.name;
        };
    }
    
    if ([segue.identifier isEqualToString:@"YTCarCerInfo"]) {
        YTCarCertificateViewController *cer = segue.destinationViewController;
        cer.isUpdate = false;
        cer.viewModel = self.viewModel;
    }
    
    if ([segue.identifier isEqualToString:@"YTAssociateDriver"]) {
        YTAssociateDriverViewController *associate = segue.destinationViewController;
        associate.callBackAction = ^(YTTeamDriverModel *model) {
            self.driverName.text = model.contactName;
            self.viewModel.driver_id = model.driverId;
        };
    }
}

@end
